package database.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.crypto.Mac;

import database.pojo.MachineInfo;

public class MachineInfoDao {

	public MachineInfo update(MachineInfo machineInfo, Connection conn) throws SQLException{
		String sqlUpdate = "update MachineInfo set QRCodeUrl = ? where MachineId = ? ";
		PreparedStatement pstmt = null;
		try{
			pstmt = conn.prepareStatement(sqlUpdate);
			pstmt.setString(1, machineInfo.getQrCodeUrl());
			pstmt.setInt(2, machineInfo.getMcahineId());
			pstmt.executeUpdate();
		}
		finally{
			if(pstmt != null){
				pstmt.close();
			}
		}
		return machineInfo;
	}
		

	public MachineInfo selectById(int machineId, Connection conn) throws SQLException{
		String sqlSelect = "select * from MachineInfo where MachineId = ? ";
		//System.out.println("try select: " + machineId);
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		MachineInfo machineInfo = null;
		try{
			pstmt = conn.prepareStatement(sqlSelect);
			pstmt.setInt(1, machineId);
			rs = pstmt.executeQuery();
			if(rs.next()){
				machineInfo = new MachineInfo();
				machineInfo.setQrCodeUrl(rs.getString("QRCodeUrl"));
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		finally{
			if(rs != null){
				rs.close();
			}
			if(pstmt != null){
				pstmt.close();
			}

		}
		return machineInfo;
	}
	
	public List<MachineInfo> selectAll(Connection conn) throws SQLException{
		String sqlSelect = "select * from MachineInfo where MachineId > 0";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MachineInfo> machineList = new ArrayList<MachineInfo>();
		MachineInfo machineInfo = null;
		try{
			pstmt = conn.prepareStatement(sqlSelect);
			rs = pstmt.executeQuery();
			while (rs.next()){
				machineInfo = new MachineInfo();
				int machineId = rs.getInt("MachineId");
				machineInfo.setMcahineId(machineId);
				machineInfo.setQrCodeUrl(rs.getString("QRCodeUrl"));
				machineList.add(machineInfo);
			}
		}
		finally{
			if(rs != null){
				rs.close();
			}
			if(pstmt != null){
				pstmt.close();
			}
		}
		return machineList;
	}


	public boolean midISActivate(Connection conn, int machineId) throws SQLException {
		String sqlSelect = "select STATUS from MachineInfo where MachineId = ? ";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{
			pstmt = conn.prepareStatement(sqlSelect);
			pstmt.setInt(1, machineId);
			rs = pstmt.executeQuery();
			if(rs.next()){
				if(rs.getInt("STATUS")==1)
					return true;
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		finally{
			if(rs != null){
				rs.close();
			}
			if(pstmt != null){
				pstmt.close();
			}

		}
		return false;
	}


	public void updateMidOnline(Connection conn, int machineId, int online) throws SQLException {
		String sqlUpdate = "update MachineInfo set ON_LINE = ? where MachineId = ? ";
		PreparedStatement pstmt = null;
		try{
			pstmt = conn.prepareStatement(sqlUpdate);
			pstmt.setInt(1,online);
			pstmt.setInt(2, machineId);
			pstmt.executeUpdate();
		}
		finally{
			if(pstmt != null){
				pstmt.close();
			}
		}		
	}
}
